#include "wavechart.h"
#include "ui_wavechart.h"
#include <algorithm>

WaveChart::WaveChart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WaveChart)
{
    ui->setupUi(this);
    //


}
//void show();

WaveChart::~WaveChart()
{
    delete ui;
}

void WaveChart::show(QJsonArray& dataArr)
{
    QVector<double> x, y;
    double min2 = dataArr[0].toDouble();
    double max2 = dataArr[0].toDouble();
    qDebug() << "min="<<min2<<", max="<<max2;
    for (int i=0; i<dataArr.size(); ++i)
    {
      x.append(i);
      double value = dataArr.at(i).toDouble();
      min2 = std::min(min2, value);
      max2 = std::max(max2, value);
      y.append(value);
    }
    // create graph and assign data to it:
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setData(x, y);
    // give the axes some labels:
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");
    // set axes ranges, so we see all data:
    ui->customPlot->xAxis->setRange(0, dataArr.size());
    ui->customPlot->yAxis->setRange(min2, max2);
    ui->customPlot->replot();
    //
    //展示界面
    QWidget::show();
}
